package com.vitabuybuy.tv.ui.mine.talent.notice.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.vitabuybuy.tv.BR
import com.library.common.BuildConfig
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.NoticeDetailDto
import com.vitabuybuy.tv.dto.TaskPoolDto
import com.vitabuybuy.tv.inter.OnTaskItemClickListener
import com.library.common.base.BaseViewModel
import com.library.common.base.LoadingDialogEntity
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.NetUrl
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.NoticeRepository
import com.vitabuybuy.tv.network.TalentLiveRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class NoticeViewModel : BaseViewModel() {

    val itemOnClickListener = object : OnTaskItemClickListener {
        override fun onItemClick(item: TaskPoolDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    val taskList = ObservableArrayList<TaskPoolDto>()

    val taskBinding: OnItemBind<TaskPoolDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: TaskPoolDto ->
            itemBinding.set(BR.itemBean, R.layout.item_task).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getNoticePoolData(pageNumber: Int, params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                val it = NoticeRepository.getNoticeList(params).await()
                if (pageNumber == 1) {
                    taskList.clear()
                }
                val list = ArrayList<TaskPoolDto>()
                for (dto in it) {
                    dto.task.id = dto.id
                    list.add(dto.task)
                }
                taskList.addAll(list)
                if (taskList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val noticeDetail = MutableLiveData<NoticeDetailDto>()

    fun getNoticDetails(noticeId: Int?) {
        rxHttpRequest {
            onRequest = {
                noticeDetail.value = NoticeRepository.getNoticeDetails(noticeId).await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun applyAuth(params: Map<String, Any?>) {
        rxHttpRequest {
            onRequest = {
                NoticeRepository.applyAuth(params).await()
                defUI.msgEvent.postValue(MessageModel(1))

            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun uploadImage(paths: List<String>) {
        defUI.loading.value = LoadingDialogEntity(isShow = true)
        val images = java.util.ArrayList<String>()
        for (file in paths) {
            rxHttpRequest {
                onRequest = {
                    val it = AuthRepository.upLoadImage(file).await()
                    images.add(NetUrl.FILE_PROFILE + it)
                    if (images.size == paths.size) {
                        defUI.loading.value = LoadingDialogEntity(isShow = false)
                        defUI.msgEvent.postValue(
                            MessageModel(2, obj = images)
                        )
                    }
                }
                onError = {
                    XToast.normal(it.msg)
                    defUI.loading.value = LoadingDialogEntity(isShow = true)
                }
            }
        }
    }

    fun comfirmReceiver(params: Map<String, Any?>) {
        rxHttpRequest {
            onRequest = {
                NoticeRepository.comfirmReceiver(params).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

}
